package com.fgs.common.application;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.webkit.WebView;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.fgs.common.BuildConfig;
import com.fgs.common.other.RegionData;
import com.fgs.common.manager.CommonSharedPreferenceManager;
import com.fgs.common.utils.EquipmentUtil;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.utils.PackageUtil;
import com.fgs.common.utils.ToastUtil;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.ToastBlackStyle;
import com.xuexiang.xui.XUI;
import com.xuexiang.xutil.XUtil;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
 * @author fgs
 * @date 2020/3/27
 **/

/**
 * Application类 初始化各种配置
 */
public class CommonApplication extends MultiDexApplication {

    private static Context mContext;//全局上下文对象

    @Override
    public void onCreate() {
        super.onCreate();

//        registerActivityLifecycleCallbacks(new CommonActivityLifecycleCallbacks());

        mContext = getApplicationContext();

        XUI.init(this); //初始化UI框架
        XUI.debug(BuildConfig.DEBUG);  //开启UI框架调试日志
        XUtil.init(this);

//        disableAPIDialog();

        //初始化省市区数据
        RegionData.getInstance().initData();

        ToastUtils.init(this,new ToastBlackStyle(this));
        ToastUtils.setGravity(Gravity.CENTER,0,300);

//        //因为Android P行为变更，不可多进程使用同一个目录webView，需要为不同进程webView设置不同目录
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
//            String processName = getProcessName(this);
//            if (!"com.fgs.common".equals(processName)) {//判断不等于默认进程名称
//                WebView.setDataDirectorySuffix(processName);
//            }
//        }

//        StringBuilder stringBuilder = new StringBuilder();
////        stringBuilder.append("manufacturer:");
//        stringBuilder.append(EquipmentUtil.getDeviceBrand());
//        stringBuilder.append(";");
////        stringBuilder.append("model:");
//        stringBuilder.append(EquipmentUtil.getSystemModel());
//        stringBuilder.append(";");
////        stringBuilder.append("version:");
//        stringBuilder.append(EquipmentUtil.getSystemVersion());
//        stringBuilder.append(";");
////        stringBuilder.append("app:");
//        stringBuilder.append(PackageUtil.getVersionName(getApplicationContext()));
//        stringBuilder.append(";");
//        stringBuilder.append("IMEI:");
////        stringBuilder.append(EquipmentUtil.getIMEI(getApplicationContext()));
//        stringBuilder.append(";");
//        CommonSharedPreferenceManager.getInstance().setPhoneInfo(stringBuilder.toString());

    }

    public String getProcessName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }

    public static Context getContext() {
        return mContext;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    /**
     * 反射 禁止弹窗
     */
    private void disableAPIDialog() {
        if (Build.VERSION.SDK_INT < 28) return;
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class clazz = Class.forName("android.app.ActivityThread");
            Method currentActivityThread = clazz.getDeclaredMethod("currentActivityThread");
            currentActivityThread.setAccessible(true);
            Object activityThread = currentActivityThread.invoke(null);
            Field mHiddenApiWarningShown = clazz.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
